From 9a387087cc8d33a00fbaea480af3cd64b9a6c051 Mon Sep 17 00:00:00 2001 From: Aku Kotkavuo Date: Mon, 13 Oct 2014 03:05:41 +0300 Subject: [PATCH] Check crate name for invalid characters in cargo new --- src/cargo/ops/cargo_new.rs | 6 ++++++ tests/test_cargo_new.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 79191ecaa..856603bfe 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -29,6 +29,12 @@ pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> { path.display()))) } let name = path.filename_str().unwrap(); + for c in name.chars() { + if c.is_alphanumeric() { continue } + if c == '_' || c == '-' { continue } + return Err(human(format!("Invalid character `{}` in crate name: `{}`", + c, name).as_slice())); + } mk(&path, name, &opts).chain_error(|| { human(format!("Failed to create project `{}` at `{}`", name, path.display())) diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index 58b5c99a9..cc2184838 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -100,6 +100,12 @@ test!(existing { dst.display()))); }) +test!(invalid_characters { + assert_that(cargo_process("new").arg("foo.rs"), + execs().with_status(101) + .with_stderr("Invalid character `.` in crate name: `foo.rs`")); +}) + test!(finds_author_user { // Use a temp dir to make sure we don't pick up .cargo/config somewhere in // the hierarchy -- 2.30.2